﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data.Common;

/// <summary>
/// Summary description for TranslationFinder2
/// </summary>

[System.Web.Script.Services.ScriptService]
public class TranslationFinder : System.Web.Services.WebService {

    /// <summary>
    /// Handles AJAX requests to find translations.
    /// </summary>
    /// <param name="language">The language of the translation entered.</param>
    /// <param name="translation">The translation to match against.</param>
    /// <returns>A List of languages/translations/comments</returns>
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    [WebMethod]
    public List<Translation> FindTranslation(string language, string translation) {
        List<Translation> translationIds = new List<Translation>();
        Database db = new Database();
        db.OpenConnection();
        using(Database.Command cmd = db.CreateCommand()) {
            cmd.CommandText = "SELECT translations.translationid, translation, comment FROM translations LEFT JOIN translationcomments ON translations.translationid = translationcomments.translationid WHERE language = @language AND translation LIKE @translation";
            cmd.AddParameterWithValue("@language", HttpUtility.UrlDecode(language));
            cmd.AddParameterWithValue("@translation", '%' + HttpUtility.UrlDecode(translation) + '%');
            DbDataReader reader = cmd.ExecuteReader();
            int limit = 5,
                found = 0;
            while(reader.Read() && found < limit) {
                translationIds.Add(new Translation(HttpUtility.UrlEncode(reader[0].ToString()), HttpUtility.UrlEncode(reader[1].ToString()), HttpUtility.UrlEncode(reader[2].ToString())));
                found++;
            }
        }
        db.CloseConnection();
        //foreach(KeyValuePair<string, object> s in translationIds) {System.Diagnostics.Debug.Write(s+"-"+s.Value.);
        return translationIds;
    }

    /// <summary>
    /// A class used by the translation finder service which can hold translation data
    /// </summary>
    public class Translation {
        public string ID { get; set; }
        public string Text { get; set; }
        public string Comment { get; set; }

        public Translation(string id, string text, string comment) {
            this.ID = id;
            this.Text = text;
            this.Comment = comment;
        }
    }

    /// <summary>
    /// Finds translation matching an ID.
    /// </summary>
    /// <param name="translationid">The translation ID to match.</param>
    /// <returns>A key/value collection of languages/translations</returns>
    public static SortedList<string, string> FindTranslation(string translationid) {
        SortedList<string, string> translations = new SortedList<string, string>();
        Database db = new Database();
        db.OpenConnection();
        using(Database.Command cmd = db.CreateCommand()) {
            cmd.CommandText = "SELECT language, translation FROM translations WHERE translationid = @tID";
            cmd.AddParameterWithValue("@tID", translationid);
            DbDataReader reader = cmd.ExecuteReader();
            while(reader.Read()) {
                translations.Add(reader[0].ToString(), reader[1].ToString());
            }
        }
        db.CloseConnection();
        return translations;
    }
    
}

